package com.jy.modules.cims.interact;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.type.TypeReference;
import com.jy.modules.cims.common.CimsConstant;
import com.jy.modules.cims.common.CimsIrtUrlConstant;
import com.jy.modules.cims.common.exception.CimsException;
import com.jy.modules.cims.common.exception.CimsExceptionConstant;
import com.jy.modules.cims.common.sysUtils.CollectionsUtils;
import com.jy.modules.cims.data.common.dto.TCMmContractConstraintDTO;
import com.jy.modules.cims.data.common.dto.TCMmCreditDetailDTO;
import com.jy.modules.cims.data.dto.ApproveConfirmResultDto;
import com.jy.modules.cims.data.dto.ConfirmParamDto;
import com.jy.modules.cims.data.dto.InteractEnsureDto;
import com.jy.modules.cims.data.dto.QuotaApplyParamDto;
import com.jy.modules.cims.data.dto.QuotaApplyResultDto;
import com.jy.modules.cims.data.dto.QuotaDetailUnlockParamDto;
import com.jy.modules.cims.data.dto.QuotaDetailUnlockParamDto.LoanUnlockInfo;
import com.jy.modules.cims.data.dto.QuotaDetailUnlockResultDto;
import com.jy.modules.cims.data.dto.QuotaDto;
import com.jy.modules.cims.data.dto.credit.RedeemParamDto;
import com.jy.modules.cims.data.dto.credit.RedeemResultDto;
import com.jy.modules.cims.data.dto.loan.LoanFundOutDto;
import com.jy.modules.cims.data.dto.loan.LoanFundOutResultDto;
import com.jy.modules.cims.data.dto.loan.LoanOfferResultDto;
import com.jy.modules.cims.data.dto.loan.LoanResultDetail;
import com.jy.modules.cims.data.dto.masterdata.ResRepaymentPlanBean;
import com.jy.modules.cims.data.interact.core.CoreStatusRequest;
import com.jy.modules.cims.data.interact.core.CoreStatusResponse;
import com.jy.modules.cims.data.interact.core.loan.request.CreditConstraint;
import com.jy.modules.cims.data.interact.core.loan.request.OperationInfo;
import com.jy.modules.cims.data.interact.core.loan.request.ReportQueryDTO;
import com.jy.modules.cims.data.interact.core.loan.request.Request2011DTO;
import com.jy.modules.cims.data.interact.core.loan.request.Request4001DTO;
import com.jy.modules.cims.data.interact.core.loan.request.Request4002DTO;
import com.jy.modules.cims.data.interact.core.loan.request.Request4003DTO;
import com.jy.modules.cims.data.interact.core.loan.request.Request4004DTO;
import com.jy.modules.cims.data.interact.core.loan.request.Request4005DTO;
import com.jy.modules.cims.data.interact.core.loan.request.Request4007DTO;
import com.jy.modules.cims.data.interact.core.loan.request.Request4008DTO;
import com.jy.modules.cims.data.interact.core.loan.request.Request4009DTO;
import com.jy.modules.cims.data.interact.core.loan.request.Request4010DTO;
import com.jy.modules.cims.data.interact.core.loan.request.Request4012DTO;
import com.jy.modules.cims.data.interact.core.loan.request.Request4037DTO;
import com.jy.modules.cims.data.interact.core.loan.request.Request8001DTO;
import com.jy.modules.cims.data.interact.core.loan.request.Request8003DTO;
import com.jy.modules.cims.data.interact.core.loan.request.Request8011DTO;
import com.jy.modules.cims.data.interact.core.loan.request.RequestLoanBuyBackDTO;
import com.jy.modules.cims.data.interact.core.loan.request.RequestUrlDataDTO;
import com.jy.modules.cims.data.interact.core.loan.request.RequsetMasterdataDTO;
import com.jy.modules.cims.data.interact.core.loan.response.AdanceRepaymentDTO;
import com.jy.modules.cims.data.interact.core.loan.response.BankAcctChangeDTO;
import com.jy.modules.cims.data.interact.core.loan.response.ImgUrl;
import com.jy.modules.cims.data.interact.core.loan.response.LoanData;
import com.jy.modules.cims.data.interact.core.loan.response.NoWithholdingActiveRepaymentDTO;
import com.jy.modules.cims.data.interact.core.loan.response.ResponeMasterdataDTO;
import com.jy.modules.cims.data.interact.core.loan.response.Response2011DTO;
import com.jy.modules.cims.data.interact.core.loan.response.Response4001DTO;
import com.jy.modules.cims.data.interact.core.loan.response.Response4002DTO;
import com.jy.modules.cims.data.interact.core.loan.response.Response4003DTO;
import com.jy.modules.cims.data.interact.core.loan.response.Response4004DTO;
import com.jy.modules.cims.data.interact.core.loan.response.Response4005DTO;
import com.jy.modules.cims.data.interact.core.loan.response.Response4007DTO;
import com.jy.modules.cims.data.interact.core.loan.response.Response4008DTO;
import com.jy.modules.cims.data.interact.core.loan.response.Response4009DTO;
import com.jy.modules.cims.data.interact.core.loan.response.Response4010DTO;
import com.jy.modules.cims.data.interact.core.loan.response.Response4012DTO;
import com.jy.modules.cims.data.interact.core.loan.response.Response4037DTO;
import com.jy.modules.cims.data.interact.core.loan.response.Response8001DTO;
import com.jy.modules.cims.data.interact.core.loan.response.Response8003DTO;
import com.jy.modules.cims.data.interact.core.loan.response.Response8011DTO;
import com.jy.modules.cims.data.interact.core.loan.response.SettlementDTO;
import com.jy.modules.cims.data.interact.core.loan.response.UrlData;
import com.jy.platform.core.message.ResponseMsg;

/**
 * <b>类名称：</b>CIRLoanIFImpl <br/>
 * <b>类描述：贷款相关交互实现类</b><br/>
 * <b>创建人：</b>chenguixue<br/>
 * <b>修改人：</b><br/>
 * <b>修改时间：</b>2015-1-20 下午5:13:15<br/>
 * <b>修改备注：</b><br/>
 * <b>版本信息：</b>v1.0.0<br/>
 */
@Service("com.jy.modules.cims.interact.CIRLoanIFImpl")
public class CIRLoanIFImpl {
	
	 private static final Logger logger = LoggerFactory.getLogger(CIRLoanIFImpl.class);
	
	@Autowired
	@Qualifier("com.jy.modules.cims.interact.InteractClient")
	public InteractClient interactClient;
	

	@Autowired
	@Qualifier("com.jy.modules.cims.interact.CInteractAssist")
	public IInteractAssist iInteractAssist;

	/**
	 * 贷款额度申请
	 * 
	 * @param paramDto
	 * @return QuotaApplyResultDto
	 * @author chenguixue
	 * @date 2015-1-21-下午5:33:37
	 * @update
	 */
	public QuotaApplyResultDto quotaApply(QuotaApplyParamDto paramDto) {
		Request4001DTO req = new Request4001DTO();
		QuotaDto quotaDto = paramDto.getQuotaDto();
		req.setInterfaceNo((String) CimsIrtUrlConstant.interactCfg.get(CimsIrtUrlConstant.IRT_QUOTAAPPLY_LOAN).get(CimsIrtUrlConstant.IRT_CODE));
		req.setBusiCode((String) CimsIrtUrlConstant.interactCfg.get(CimsIrtUrlConstant.IRT_QUOTAAPPLY_LOAN).get(CimsIrtUrlConstant.IRT_BUSICODE));
		req.setCreditCode(quotaDto.getQuotaCode());
		req.setStartDate(quotaDto.getStartDate());
		req.setEndDate(quotaDto.getInvalidDate());
		req.setCreditAmount(quotaDto.getQuotaSum());
		if (CimsIrtUrlConstant.IRT_QUOTAAPPLY_OPERTYPE_NEW.equals(paramDto.getOperType()))
			req.setOperType(CimsIrtUrlConstant.IRT_QUOTAAPPLY_LOAN_TYPE_NEW);
		else if (CimsIrtUrlConstant.IRT_QUOTAAPPLY_OPERTYPE_CANCEL.equals(paramDto.getOperType()))
			req.setOperType(CimsIrtUrlConstant.IRT_QUOTAAPPLY_LOAN_TYPE_CANCEL);
		req.setCorpBrchCode(quotaDto.getCooperorgCode());
		req.setCorpBrchName(quotaDto.getCooperOrgName());

		List<CreditConstraint> ckDataList = new ArrayList<CreditConstraint>();
		req.setCkDataList(ckDataList);
		List<TCMmContractConstraintDTO> list = paramDto.getContractConstraintDtoList();
		if (list != null) {
			for (TCMmContractConstraintDTO value : list) {
				CreditConstraint cc = new CreditConstraint();
				cc.setConstraintCode(value.getConstraintCode());
				cc.setConstraintName(value.getConstraintName());
				cc.setConstraintValues(value.getConstraintValues());
				cc.setScopeEquation(value.getScopeEquation());
				ckDataList.add(cc);
			}
		}
		InteractEnsureDto ensureDto = new InteractEnsureDto();
		ensureDto.setDealCode(paramDto.getDealCode());
		Response4001DTO response = null;
		response = interactClient.sendReqToCore(
				(String) CimsIrtUrlConstant.interactCfg.get(CimsIrtUrlConstant.IRT_QUOTAAPPLY_LOAN).get(CimsIrtUrlConstant.IRT_URL), req,
				new TypeReference<ResponseMsg<Response4001DTO>>() {
				}, ensureDto);
		if (response != null)
			return QuotaApplyResultDto.successQuotaApplyResultDto();
		return null;
	}

	/**
	 * 异步获取贷款匹配信息
	 * 
	 * @param paramDto
	 * @return
	 * @return QuotaApplyResultDto
	 * @author chenguixue
	 * @date 2015-1-27-上午11:10:30
	 * @update
	 */
	public QuotaApplyResultDto getQuotaApplyResult(QuotaApplyParamDto paramDto) {

		Request4002DTO req = new Request4002DTO();
		req.setInterfaceNo((String) CimsIrtUrlConstant.interactCfg.get(CimsIrtUrlConstant.IRT_QUOTAAPPLY_LOAN_RESULT)
				.get(CimsIrtUrlConstant.IRT_CODE));
		req.setBusiCode((String) CimsIrtUrlConstant.interactCfg.get(CimsIrtUrlConstant.IRT_QUOTAAPPLY_LOAN_RESULT).get(
				CimsIrtUrlConstant.IRT_BUSICODE));

		req.setCreditCode(paramDto.getQuotaDto().getQuotaCode());
		req.setCurPage(paramDto.getCurPage());
		req.setPageSize(paramDto.getPageSize());
		req.setTotalRows(paramDto.getTotalRows());

		Response4002DTO response = null;

		InteractEnsureDto ensureDto = new InteractEnsureDto();
		ensureDto.setDealCode(paramDto.getDealCode());

		response = interactClient.sendReqToCore(
				(String) CimsIrtUrlConstant.interactCfg.get(CimsIrtUrlConstant.IRT_QUOTAAPPLY_LOAN_RESULT).get(CimsIrtUrlConstant.IRT_URL), req,
				new TypeReference<ResponseMsg<Response4002DTO>>() {
				}, ensureDto);
		if (response != null) {
			QuotaApplyResultDto dto = QuotaApplyResultDto.successQuotaApplyResultDto();
			dto.setTotalRows(response.getTotalRows());
			dto.setLoanList(response.getLoanInfoDataList());
			return dto;
		}
		return null;
	}

	/**
	 * 贷款额度释放
	 * 
	 * @param unlockDto
	 * @return QuotaUnlockResultDto
	 * @author chenguixue
	 * @date 2015-1-21-下午5:32:51
	 * @update
	 */
	public QuotaDetailUnlockResultDto quotaDetailUnlock(QuotaDetailUnlockParamDto unlockDto) {
		Request4003DTO req = new Request4003DTO();
		List<LoanUnlockInfo> unlockList = unlockDto.getUnlockList();
		req.setInterfaceNo((String) CimsIrtUrlConstant.interactCfg.get(CimsIrtUrlConstant.IRT_LOAN_UNLOCK).get(CimsIrtUrlConstant.IRT_CODE));
		req.setBusiCode((String) CimsIrtUrlConstant.interactCfg.get(CimsIrtUrlConstant.IRT_LOAN_UNLOCK).get(CimsIrtUrlConstant.IRT_BUSICODE));

		List<OperationInfo> dtoDataList = new ArrayList<OperationInfo>();
		req.setDtoDataList(dtoDataList);

		if (unlockList != null && unlockList.size() > 0) {
			for (LoanUnlockInfo value : unlockList) {
				OperationInfo cc = new OperationInfo();
				cc.setContractNo(value.getContractNo());
				cc.setOperType(value.getUnlockType());
				cc.setOperReason(value.getUnlockReason());
				dtoDataList.add(cc);
			}
			InteractEnsureDto ensureDto = new InteractEnsureDto();
			ensureDto.setDealCode(unlockDto.getDealCode());
			Response4003DTO response = null;
			response = interactClient.sendReqToCore(
					(String) CimsIrtUrlConstant.interactCfg.get(CimsIrtUrlConstant.IRT_LOAN_UNLOCK).get(CimsIrtUrlConstant.IRT_URL), req,
					new TypeReference<ResponseMsg<Response4003DTO>>() {
					}, ensureDto);
			if (response != null)
				return QuotaDetailUnlockResultDto.successQuotaDetailUnlockResultDto();
		}
		return QuotaDetailUnlockResultDto.successQuotaDetailUnlockResultDto();
	}

	/**
	 * 贷款审批确认
	 * 
	 * @param approveDto
	 * @return LoanApproveConfirmResultDto
	 * @author chenguixue
	 * @date 2015-1-21-下午5:32:05
	 * @update
	 */
	public ApproveConfirmResultDto loanApproveConfrim(ConfirmParamDto approveDto) {
		Request4004DTO req = new Request4004DTO();
		List<ConfirmParamDto.LoanApproveInfo> approveList = approveDto.getApproveList();
		req.setInterfaceNo((String) CimsIrtUrlConstant.interactCfg.get(CimsIrtUrlConstant.IRT_LOAN_APPROVE).get(CimsIrtUrlConstant.IRT_CODE));
		req.setBusiCode((String) CimsIrtUrlConstant.interactCfg.get(CimsIrtUrlConstant.IRT_LOAN_APPROVE).get(CimsIrtUrlConstant.IRT_BUSICODE));

		List<OperationInfo> dtoDataList = new ArrayList<OperationInfo>();
		req.setDtoDataList(dtoDataList);

		if (approveList != null) {
			for (ConfirmParamDto.LoanApproveInfo value : approveList) {
				OperationInfo cc = new OperationInfo();
				cc.setContractNo(value.getContractNo());
				cc.setOperType(value.getApproveType());
				cc.setOperReason(value.getApproveReason());
				dtoDataList.add(cc);
			}
		}
		InteractEnsureDto ensureDto = new InteractEnsureDto();
		ensureDto.setDealCode(approveDto.getDealCode());
		Response4004DTO response = null;
		response = interactClient.sendReqToCore(
				(String) CimsIrtUrlConstant.interactCfg.get(CimsIrtUrlConstant.IRT_LOAN_APPROVE).get(CimsIrtUrlConstant.IRT_URL), req,
				new TypeReference<ResponseMsg<Response4004DTO>>() {
				}, ensureDto);
		if (response != null) {
			return ApproveConfirmResultDto.successApproveConfirmResultDto();
		}
		return null;
	}

	/**
	 * 贷款放款确认
	 * 
	 * @param loanFundOutDto
	 * @return LoanFundOutResultDto
	 * @author chenguixue
	 * @date 2015-3-2-下午7:11:33
	 * @update
	 */
	public LoanFundOutResultDto loanFundOut(LoanFundOutDto loanFundOutDto) {
		Request4005DTO req = new Request4005DTO();
		List<LoanFundOutDto.LoanFundOutInfo> loanFundOutList = loanFundOutDto.getLoanFundOutInfoList();
		req.setInterfaceNo((String) CimsIrtUrlConstant.interactCfg.get(CimsIrtUrlConstant.IRT_LOAN_FUNDOUT).get(CimsIrtUrlConstant.IRT_CODE));
		req.setBusiCode((String) CimsIrtUrlConstant.interactCfg.get(CimsIrtUrlConstant.IRT_LOAN_FUNDOUT).get(CimsIrtUrlConstant.IRT_BUSICODE));

		List<OperationInfo> dtoDataList = new ArrayList<OperationInfo>();

		if (loanFundOutList != null) {
			for (LoanFundOutDto.LoanFundOutInfo value : loanFundOutList) {
				OperationInfo cc = new OperationInfo();
				cc.setContractNo(value.getContractNo());
				cc.setOperType(value.getOperType());
				cc.setOperReason(value.getOperReason());
				cc.setPayDate(value.getPayDate());
				dtoDataList.add(cc);
			}
		}
		req.setDtoDataList(dtoDataList);
		InteractEnsureDto ensureDto = new InteractEnsureDto();
		ensureDto.setDealCode(loanFundOutDto.getDealCode());
		Response4005DTO response = null;
		response = interactClient.sendReqToCore(
				(String) CimsIrtUrlConstant.interactCfg.get(CimsIrtUrlConstant.IRT_LOAN_FUNDOUT).get(CimsIrtUrlConstant.IRT_URL), req,
				new TypeReference<ResponseMsg<Response4005DTO>>() {
				}, ensureDto);
		if (response != null) {
			return LoanFundOutResultDto.successLoanFundOutResultDto();
		}
		return null;
	}

	/**
	 * @description 获取贷前系统贷款原始信息
	 * 
	 * @author shengzhoutao
	 * @param busiNoForInteract
	 *            交易流水号
	 * @param contractNo
	 *            合同号
	 * @return List<LoanData>
	 */
	public List<LoanData> getBefLoanResult(String busiNoForInteract, String contractNo) {
		Request8001DTO req = new Request8001DTO();
		req.setInterfaceNo((String) CimsIrtUrlConstant.interactCfg.get(CimsIrtUrlConstant.IRT_LOAN_INFO).get(CimsIrtUrlConstant.IRT_CODE));
		req.setBusiCode((String) CimsIrtUrlConstant.interactCfg.get(CimsIrtUrlConstant.IRT_LOAN_INFO).get(CimsIrtUrlConstant.IRT_BUSICODE));
		req.setContractNo(contractNo);

		Response8001DTO response = null;

		InteractEnsureDto ensureDto = new InteractEnsureDto();
		ensureDto.setDealCode(busiNoForInteract);

		response = interactClient.sendReqToBefLoan(
				(String) CimsIrtUrlConstant.interactCfg.get(CimsIrtUrlConstant.IRT_LOAN_INFO).get(CimsIrtUrlConstant.IRT_URL), req,
				new TypeReference<ResponseMsg<Response8001DTO>>() {
				}, ensureDto);

		String status = response.getStatus();
		if(!"10000".equals(status)){
			throw new CimsException("获取贷前系统贷款原始信息异常：" + response.getRespMsg());
		}
		ensureDto = null;

		return response.getLoanDataList();
	}
	
	/**
	 * @description 押品系统附件表信息查询
	 * 
	 * @author zhoupeng
	 * @param collateralDataList
	 *           押品编号集合
	 * @return List<collateralDataList>
	 */
	public Response2011DTO getBefCollateralResult(List<String> collateralDataList) {
		Request2011DTO req = new Request2011DTO();
		String busiNoForInteract = iInteractAssist.getBusiNoForInteract();
		req.setInterfaceNo((String) CimsIrtUrlConstant.interactCfg.get(CimsIrtUrlConstant.IRT_COLLATERAL_INFO_QUERY).get(CimsIrtUrlConstant.IRT_CODE));
		req.setBusiCode((String) CimsIrtUrlConstant.interactCfg.get(CimsIrtUrlConstant.IRT_COLLATERAL_INFO_QUERY).get(CimsIrtUrlConstant.IRT_BUSICODE));
		req.setCollateralDataList(collateralDataList);
		Response2011DTO response = null;

		InteractEnsureDto ensureDto = new InteractEnsureDto();
		ensureDto.setDealCode(busiNoForInteract);

		response = interactClient.sendReqToCollateral(
				(String) CimsIrtUrlConstant.interactCfg.get(CimsIrtUrlConstant.IRT_COLLATERAL_INFO_QUERY).get(CimsIrtUrlConstant.IRT_URL), req,
				new TypeReference<ResponseMsg<Response2011DTO>>() {
				}, ensureDto);

		String Code = response.getRetCode();
		if(!"0000".equals(Code)){
			throw new CimsException("押品系统附件表信息异常：" + response.getRetMsg());
		}
		ensureDto = null;

		return response;
	}

	/**
	 * @description 获取贷前系统借款人相关方面的信息
	 * 
	 * @author shengzhoutao
	 * @param contractNo
	 *            合同号
	 * @return Response8002DTO
	 */
	public Response8003DTO getBorrowersResult(String contractNo) {
		String busiNoForInteract = iInteractAssist.getBusiNoForInteract();
		
		Request8003DTO req = new Request8003DTO();
		req.setInterfaceNo((String) CimsIrtUrlConstant.interactCfg.get(CimsIrtUrlConstant.IRT_BORROWERS_INFO).get(CimsIrtUrlConstant.IRT_CODE));
		req.setBusiCode((String) CimsIrtUrlConstant.interactCfg.get(CimsIrtUrlConstant.IRT_BORROWERS_INFO).get(CimsIrtUrlConstant.IRT_BUSICODE));
		req.setContractCode(contractNo);
		
		Response8003DTO response = null;

		InteractEnsureDto ensureDto = new InteractEnsureDto();
		ensureDto.setDealCode(busiNoForInteract);

		response = interactClient.sendReqToBefLoan(
				(String) CimsIrtUrlConstant.interactCfg.get(CimsIrtUrlConstant.IRT_BORROWERS_INFO).get(CimsIrtUrlConstant.IRT_URL), req,
				new TypeReference<ResponseMsg<Response8003DTO>>() {
				}, ensureDto);

		String status = response.getStatus();
		if(!"10000".equals(status)){
			throw new CimsException("获取贷前系统借款人信息异常：" + response.getRetMsg());
		}
		ensureDto = null;

		return response;
	}
	
	/**
	 * @description 获取贷前系统贷款的影像信息
	 * @author shengzhoutao
	 * @param contractNo
	 *            合同号
	 * @return List<ImgUrl>
	 */
	public List<ImgUrl> getBefLoanImgUrl(String contractNo) {
		String busiNoForInteract = iInteractAssist.getBusiNoForInteract();
		List<LoanData> LoanDataList = getBefLoanResult(busiNoForInteract, contractNo);
		if (CollectionsUtils.isEmpty(LoanDataList)) {
			return null;
		}

		List<ImgUrl> imgUrlList = new ArrayList<ImgUrl>();
		ImgUrl imgUrl = null;
		for (LoanData loanData : LoanDataList) {
			List<UrlData> urlDataList = loanData.getUrlDataList();
			if (CollectionsUtils.isEmpty(urlDataList)) {
				continue;
			}

			for (UrlData urlData : urlDataList) {
				imgUrl = new ImgUrl();
				imgUrl.setAttachCode(urlData.getAttachCode());
				imgUrl.setAttachName(urlData.getAttachName());
				imgUrl.setAttachUrl(urlData.getAttachUrl());
				imgUrl.setCustNo(urlData.getCustCode());

				imgUrlList.add(imgUrl);
			}
		}

		return imgUrlList;
	}

	/**
	 * @description 获取贷前系统贷款的信息
	 * @author shengzhoutao
	 * @param contractNo
	 *            合同号
	 * @return LoanData
	 */
	public LoanData getBefLoanInfo(String contractNo) {
		String busiNoForInteract = iInteractAssist.getBusiNoForInteract();
		List<LoanData> LoanDataList = getBefLoanResult(busiNoForInteract, contractNo);
		if (CollectionsUtils.isEmpty(LoanDataList)) {
			return null;
		}
		return LoanDataList.get(0);
	}

	/**
	 * 查询核心非代扣主动还款(认款)信息
	 * 
	 * @title: queryNoWithholdingActiveRepayment
	 * @author
	 * @description:
	 * @date 2015-3-23 上午9:41:24
	 * @param reportQueryDTO
	 * @return
	 * @throws
	 */
	public List<NoWithholdingActiveRepaymentDTO> queryNoWithholdingActiveRepayment(ReportQueryDTO reportQueryDTO) {
		Request4007DTO req = new Request4007DTO();
		req.setStartDate(reportQueryDTO.getStartDate());
		req.setEndDate(reportQueryDTO.getEndDate());
		req.setCorpOrgCode(reportQueryDTO.getCorpOrgCode());
		req.setInterfaceNo((String) CimsIrtUrlConstant.interactCfg.get(CimsIrtUrlConstant.IRT_LOAN_NOWITHHOLDINGACTIVEREPAYMEN_QUERY).get(
				CimsIrtUrlConstant.IRT_CODE));
		req.setBusiCode((String) CimsIrtUrlConstant.interactCfg.get(CimsIrtUrlConstant.IRT_LOAN_NOWITHHOLDINGACTIVEREPAYMEN_QUERY).get(
				CimsIrtUrlConstant.IRT_BUSICODE));

		InteractEnsureDto ensureDto = new InteractEnsureDto();
		ensureDto.setDealCode(reportQueryDTO.getDealCode());
		Response4007DTO response = null;
		response = interactClient.sendReqToCore(
				(String) CimsIrtUrlConstant.interactCfg.get(CimsIrtUrlConstant.IRT_LOAN_NOWITHHOLDINGACTIVEREPAYMEN_QUERY).get(
						CimsIrtUrlConstant.IRT_URL), req, new TypeReference<ResponseMsg<Response4007DTO>>() {
				}, ensureDto);
		if (response != null) {
			return response.getRecDataList();
		}
		return null;
	}

	/**
	 * 查询核心一次性结清提前还款明细
	 * 
	 * @title: queryAdvancFinish
	 * @author
	 * @description:
	 * @date 2015-3-23 上午9:41:24
	 * @param reportQueryDTO
	 * @return
	 * @throws
	 */
	public List<SettlementDTO> queryAdvancFinish(ReportQueryDTO reportQueryDTO) {
		Request4008DTO req = new Request4008DTO();
		req.setStartDate(reportQueryDTO.getStartDate());
		req.setEndDate(reportQueryDTO.getEndDate());
		req.setCorpOrgCode(reportQueryDTO.getCorpOrgCode());
		req.setInterfaceNo((String) CimsIrtUrlConstant.interactCfg.get(CimsIrtUrlConstant.IRT_LOAN_ADVANCEFINISH_QUERY).get(
				CimsIrtUrlConstant.IRT_CODE));
		req.setBusiCode((String) CimsIrtUrlConstant.interactCfg.get(CimsIrtUrlConstant.IRT_LOAN_ADVANCEFINISH_QUERY).get(
				CimsIrtUrlConstant.IRT_BUSICODE));

		InteractEnsureDto ensureDto = new InteractEnsureDto();
		ensureDto.setDealCode(reportQueryDTO.getDealCode());
		Response4008DTO response = null;
		response = interactClient.sendReqToCore(
				(String) CimsIrtUrlConstant.interactCfg.get(CimsIrtUrlConstant.IRT_LOAN_ADVANCEFINISH_QUERY).get(CimsIrtUrlConstant.IRT_URL), req,
				new TypeReference<ResponseMsg<Response4008DTO>>() {
				}, ensureDto);
		if (response != null) {
			return response.getSettlementDataList();
		}
		return null;
	}

	/**
	 * 查询核心银行账户变更信息
	 * 
	 * @title: queryBankAcctChange
	 * @author
	 * @description:
	 * @date 2015-3-23 上午9:41:24
	 * @param reportQueryDTO
	 * @return
	 * @throws
	 */
	public List<BankAcctChangeDTO> queryBankAcctChange(ReportQueryDTO reportQueryDTO) {
		Request4009DTO req = new Request4009DTO();
		req.setStartDate(reportQueryDTO.getStartDate());
		req.setEndDate(reportQueryDTO.getEndDate());
		req.setCorpOrgCode(reportQueryDTO.getCorpOrgCode());
		req.setInterfaceNo((String) CimsIrtUrlConstant.interactCfg.get(CimsIrtUrlConstant.IRT_LOAN_BANKACCCHANGE_QUERY).get(
				CimsIrtUrlConstant.IRT_CODE));
		req.setBusiCode((String) CimsIrtUrlConstant.interactCfg.get(CimsIrtUrlConstant.IRT_LOAN_BANKACCCHANGE_QUERY).get(
				CimsIrtUrlConstant.IRT_BUSICODE));

		InteractEnsureDto ensureDto = new InteractEnsureDto();
		ensureDto.setDealCode(reportQueryDTO.getDealCode());
		Response4009DTO response = null;
		response = interactClient.sendReqToCore(
				(String) CimsIrtUrlConstant.interactCfg.get(CimsIrtUrlConstant.IRT_LOAN_BANKACCCHANGE_QUERY).get(CimsIrtUrlConstant.IRT_URL), req,
				new TypeReference<ResponseMsg<Response4009DTO>>() {
				}, ensureDto);
		if (response != null) {
			return response.getAccChangeDataList();
		}
		return null;
	}

	/**
	 * 查询核心预缴提前还款明细信息
	 * 
	 * @title: queryAdvanceRepayment
	 * @author
	 * @description:
	 * @date 2015-3-23 上午9:41:24
	 * @param reportQueryDTO
	 * @return
	 * @throws
	 */
	public List<AdanceRepaymentDTO> queryAdvanceRepayment(ReportQueryDTO reportQueryDTO) {
		Request4010DTO req = new Request4010DTO();
		req.setStartDate(reportQueryDTO.getStartDate());
		req.setEndDate(reportQueryDTO.getEndDate());
		req.setCorpOrgCode(reportQueryDTO.getCorpOrgCode());
		req.setInterfaceNo((String) CimsIrtUrlConstant.interactCfg.get(CimsIrtUrlConstant.IRT_LOAN_ADVANCEREPAYMENT_QUERY).get(
				CimsIrtUrlConstant.IRT_CODE));
		req.setBusiCode((String) CimsIrtUrlConstant.interactCfg.get(CimsIrtUrlConstant.IRT_LOAN_ADVANCEREPAYMENT_QUERY).get(
				CimsIrtUrlConstant.IRT_BUSICODE));

		InteractEnsureDto ensureDto = new InteractEnsureDto();
		ensureDto.setDealCode(reportQueryDTO.getDealCode());
		Response4010DTO response = null;
		response = interactClient.sendReqToCore(
				(String) CimsIrtUrlConstant.interactCfg.get(CimsIrtUrlConstant.IRT_LOAN_ADVANCEREPAYMENT_QUERY).get(CimsIrtUrlConstant.IRT_URL), req,
				new TypeReference<ResponseMsg<Response4010DTO>>() {
				}, ensureDto);
		if (response != null) {
			return response.getRepaymentDataList();
		}
		return null;
	}
	
	/**
	 * 查询核心报盘结果信息
	 * 
	 * @author
	 * @description:
	 * @return
	 * @throws
	 */
	public List<LoanResultDetail> queryLoanOfferResultRepayment(LoanOfferResultDto loanOfferResultDto) throws Exception {
		List<LoanResultDetail> loanOfferResultInfo = loanOfferResultDto.getLoanResultDetail();
		
		if(loanOfferResultInfo.size()>0){
			
			Request4012DTO req = new Request4012DTO();
			req.setDtoDataList(loanOfferResultInfo);
			req.setInterfaceNo((String) CimsIrtUrlConstant.interactCfg.get(CimsIrtUrlConstant.IRT_LOAN_OFFER_RESULT_QUERY).get(
					CimsIrtUrlConstant.IRT_CODE));
			req.setBusiCode((String) CimsIrtUrlConstant.interactCfg.get(CimsIrtUrlConstant.IRT_LOAN_OFFER_RESULT_QUERY).get(
					CimsIrtUrlConstant.IRT_BUSICODE));
			
			InteractEnsureDto ensureDto = new InteractEnsureDto();
			ensureDto.setDealCode(loanOfferResultDto.getDealCode());
			Response4012DTO response = null;
			response = interactClient.sendReqToCore(
					(String) CimsIrtUrlConstant.interactCfg.get(CimsIrtUrlConstant.IRT_LOAN_OFFER_RESULT_QUERY).get(CimsIrtUrlConstant.IRT_URL), req,
					new TypeReference<ResponseMsg<Response4012DTO>>() {
					}, ensureDto);
			if (response != null) {
				return response.getDtoDataList();
			}
		}
		return null;
	}

	/**
	 * 查询核心系统日终状态，0未完成时，其他接口不可用；1日终完成。
	 * 
	 * @title: queryCoreStatus
	 * @author
	 * @description:
	 * @date 2015-3-30 下午4:14:31
	 * @param date
	 *            YYYY-MM-DD
	 * @return
	 * @throws
	 */
	public CoreStatusResponse queryCoreStatus(String date) {
		CoreStatusRequest req = new CoreStatusRequest();
		req.setReqAcctDate(date);
		req.setInterfaceNo((String) CimsIrtUrlConstant.interactCfg.get(CimsIrtUrlConstant.IRT_CORE_STATUS_QUERY).get(CimsIrtUrlConstant.IRT_CODE));
		req.setBusiCode((String) CimsIrtUrlConstant.interactCfg.get(CimsIrtUrlConstant.IRT_CORE_STATUS_QUERY).get(CimsIrtUrlConstant.IRT_BUSICODE));

		String busiNoForInteract = iInteractAssist.getInteractNo();
		InteractEnsureDto ensureDto = new InteractEnsureDto();
		ensureDto.setDealCode(busiNoForInteract);
		CoreStatusResponse response = null;
		response = interactClient.sendReqToCore(
				(String) CimsIrtUrlConstant.interactCfg.get(CimsIrtUrlConstant.IRT_CORE_STATUS_QUERY).get(CimsIrtUrlConstant.IRT_URL), req,
				new TypeReference<ResponseMsg<CoreStatusResponse>>() {
				}, ensureDto);
		if (response != null) {
			return response;
		}
		return null;
	}

	
	/**
	 * 查询主数据还款计划信息
	 * 
	 * @author
	 * @description:
	 * @return
	 * @throws
	 */
	public ResRepaymentPlanBean queryMasterdataResultRepayment(RequsetMasterdataDTO requsetMasterdataDTO) throws Exception {
		
		String url = "/api/contract/repaymentPlan/search/v1";
		
		if(requsetMasterdataDTO !=null){

			InteractEnsureDto ensureDto = new InteractEnsureDto();
			ResponeMasterdataDTO response = null;
			response = interactClient.sendReqToMasterdata(url, requsetMasterdataDTO,new TypeReference<ResponseMsg<ResponeMasterdataDTO>>() {
					}, ensureDto);
			if (response != null) {
				return response;
			}
		}
		return null;
	}

	
    /**
     * 贷款转出批量回购
     * 
     * @title: sendLoanTransferBatchRedeemToCore
     * @author: yanjingying
     * @description:
     * @date 2015-12-11 下午14:39:00
     * @param request
     * @return
     * @throws
     */
    public RedeemResultDto sendLoanTransferBatchRedeemToCore(RedeemParamDto redeemParamDto) throws Exception{
    	String busiNoForInteract = redeemParamDto.getFrontTransNo();
    	if(busiNoForInteract == null || "".equals(busiNoForInteract)){
    		throw new CimsException("4037贷款回购接口入参前置流水号不能为空");
    	}
    	
    	Request4037DTO req = new Request4037DTO();
        List<TCMmCreditDetailDTO> creditDetailDTOList = redeemParamDto.gettCMmCreditDetailDTOList();
        
        req.setFrontTransNo(busiNoForInteract);   //设置前置接口交易流水号
        req.setInterfaceNo((String) CimsIrtUrlConstant.interactCfg.get(CimsIrtUrlConstant.IRT_LOAN_TRANSFER_REDEEM_TO_CORE).get(CimsIrtUrlConstant.IRT_CODE));  //接口编码
        req.setBusiCode((String) CimsIrtUrlConstant.interactCfg.get(CimsIrtUrlConstant.IRT_LOAN_TRANSFER_REDEEM_TO_CORE).get(CimsIrtUrlConstant.IRT_BUSICODE)); //业务编码
        req.setBatchId(redeemParamDto.getDealCode());  //批次号
        req.setCCode(redeemParamDto.getCCode());       //合作机构代码

        if (creditDetailDTOList == null || creditDetailDTOList.isEmpty()) {
            logger.info(CimsExceptionConstant.INTERACT_LOAN_TRANSFER_REDEEM_LIST_EMPTY);
            return RedeemResultDto.successRedeemResultDto();
        }

        // 生成贷款回购的request请求列表
        List<RequestLoanBuyBackDTO> transferList = new ArrayList<RequestLoanBuyBackDTO>(creditDetailDTOList.size());
        for (TCMmCreditDetailDTO creditDetailDTO : creditDetailDTOList) {
        	RequestLoanBuyBackDTO transferDto = new RequestLoanBuyBackDTO();
        	transferDto.setContractNo(creditDetailDTO.getCreditCode());           //合同编号
        	transferDto.setDeliveryDate(creditDetailDTO.getRedeemDate());         //回购日期
        	transferDto.setMonthlyRepayAmount(creditDetailDTO.getThisRepaySum());  //每月还款金额
        	transferDto.setStatus(creditDetailDTO.getCoreCreditStatus());          //贷款状态 01-正常 95-提前结清
        	transferDto.setLoanType(CimsConstant.mapProductType.get(creditDetailDTO.getProductType()));//合同类型
            transferList.add(transferDto);
        }        
        req.setTransferList(transferList);    //放款对象
        InteractEnsureDto ensureDto = new InteractEnsureDto();
        ensureDto.setDealCode(redeemParamDto.getDealCode());
        Response4037DTO responseMsg = interactClient.sendReqToCore(
                (String) CimsIrtUrlConstant.interactCfg.get(CimsIrtUrlConstant.IRT_LOAN_TRANSFER_REDEEM_TO_CORE).get(
                        CimsIrtUrlConstant.IRT_URL), req, new TypeReference<ResponseMsg<Response4037DTO>>() {},
                ensureDto);
        if (responseMsg != null){
        	RedeemResultDto rdto = RedeemResultDto.successRedeemResultDto();
        	rdto.setFrontTransNo(responseMsg.getFrontTransNo()); //合作机构交易流水号
        	rdto.setCoreTransNo(responseMsg.getCoreTransNo());  //核心交易流水号
        	rdto.setBusiCode(responseMsg.getBusiCode());  //业务编码
        	rdto.setInterfaceNo(responseMsg.getInterfaceNo());  //接口编码
        	rdto.setRetCode(responseMsg.getRetCode());   //返回码        0000：成功  0001：失败  9999:日终处理中
        	rdto.setRetTime(responseMsg.getRetTime());   //返回时间
        	rdto.setRetMsg(responseMsg.getRetMsg());     //返回信息
        	rdto.setTransferList(responseMsg.getTransferList());
        	return rdto;
        }
        return null;
    }
   
    /**
     * 发送电子电子签章-影像回传
     * @author zhangmi
     * @date  2016年12月26日
     */
    public Response8011DTO sendReturnUrlData(List<RequestUrlDataDTO> urlDataList,String contractNo)throws Exception{
    	String busiNoForInteract = iInteractAssist.getBusiNoForInteract();
    	Request8011DTO req = new Request8011DTO();
    	req.setUrlDataList(urlDataList);
    	req.setContractNo(contractNo);
        req.setFrontTransNo(busiNoForInteract);   //设置前置接口交易流水号
        req.setInterfaceNo((String) CimsIrtUrlConstant.interactCfg.get(CimsIrtUrlConstant.IRT_LOAN_SIGN_URL_DATA_CODE).get(CimsIrtUrlConstant.IRT_CODE));  //接口编码
        req.setBusiCode((String) CimsIrtUrlConstant.interactCfg.get(CimsIrtUrlConstant.IRT_LOAN_SIGN_URL_DATA_CODE).get(CimsIrtUrlConstant.IRT_BUSICODE)); //业务编码
        InteractEnsureDto ensureDto = new InteractEnsureDto();
		ensureDto.setDealCode(busiNoForInteract);
        Response8011DTO responseMsg = interactClient.sendReqToBefLoan(
				(String) CimsIrtUrlConstant.interactCfg.get(CimsIrtUrlConstant.IRT_LOAN_SIGN_URL_DATA_CODE).get(CimsIrtUrlConstant.IRT_URL), req,
				new TypeReference<ResponseMsg<Response8011DTO>>() {
				}, ensureDto);
        if(responseMsg!=null){
        	return responseMsg;
        }
        return null;
    }
}
